package cn.congzhou.crm.web.controller;


import cn.congzhou.crm.domain.Department;
import cn.congzhou.crm.exception.ProhibitDeleteException;
import cn.congzhou.crm.query.QueryObject;
import cn.congzhou.crm.query.ResultGridData;
import cn.congzhou.crm.service.IDepartmentService;
import cn.congzhou.crm.utils.ResultJSON;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    private Logger logger = LoggerFactory.getLogger(DepartmentController.class);

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        PageInfo<Department> pageInfo = departmentService.selectByPage(qo);
        model.addAttribute("ro",pageInfo);

        return "/department/list";
    }


    @RequestMapping("/input")
    @RequiresPermissions(value = {"编辑部门信息","department:input"},logical = Logical.OR)
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("entity", departmentService.selectByPrimaryKey(id));
        }

        return "/department/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"添加/保存部门信息","department:saveOrUpdate"},logical = Logical.OR)
    @ResponseBody
    public Object saveOrUpdate(Department dept) {
        ResultJSON result = new ResultJSON();
        try {
            departmentService.saveOrUpdate(dept);
            logger.info("保存部门信息成功");
        } catch (Exception e) {
            logger.error("保存部门信息出错",e);
            result.mark("保存失败: " + e.getMessage());
        }

        return result;
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"删除部门","department:delete"},logical = Logical.OR)
    @ResponseBody
    public Object delete(Long id) {
        ResultJSON result = new ResultJSON();
        try {
            departmentService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            logger.error("删除部门信息出错",e);
            result.mark("删除失败");
        }

        return result;
    }



    //==================================================下面是ligerUI整改后方法================================================================


    @RequestMapping(value = "toDepartmentGrid",method = RequestMethod.GET)
    @RequiresPermissions(value = {"查询部门信息","department:query"},logical = Logical.OR)
    public String toDepartmentGrid(){
        return "/department/departmentGrid";
    }

    @ResponseBody
    @RequestMapping(value = "/getDepartmentData",method = RequestMethod.POST)
    @RequiresPermissions(value = {"查询部门信息","department:query"},logical = Logical.OR)
    public Object getDepartmentData(){
        List<Department> allDept = departmentService.selectAll();
        Map<String,Object> map = new HashMap<>();
        map.put("list",allDept);
        return map;
    }

    @ResponseBody
    @RequestMapping("/deleteDepartment")
    @RequiresPermissions(value = {"删除部门信息","department:delete"},logical = Logical.OR)
    public Object deleteDepartment(Long[] ids){
        ResultJSON result = new ResultJSON();
        if(ids == null || ids.length == 0){
            result.mark("删除部门信息失败，请选择要删除的数据");
            return result;
        }
        try {
            departmentService.batchDeleteDepartment(ids);
        }catch (ProhibitDeleteException e){
            result.mark(e.getMessage());
        } catch (Exception e){
            result.mark("删除部门出错" + e.getMessage());
            logger.error("删除部门信息出错"+ e.getMessage(), e);
        }

        return result;
    }

}
